package database.store;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

import database.PrepareFiles;

import user.blog.BlogManagerIF;
import user.blog.FlagsPost;

/**
 * Class that receive the blog information, .xml file. Each blog has a .xml file
 * with UID in the type "title.xml". This files will be stored in: Blogs/title.xml
 * for each Blog stored.
 * 
 * @author Cassio Espindola - cassioafonso@gmail.com
 * 
 */
public class StoreBlog implements BlogManagerIF {
	private static StoreBlog thisInstance;
	private String login;
	private HashMap<String, Properties> listBlogs;

	/**
	 * A constructor of a StoreBlog. Initialize some variables with default
	 * value empty (not null). This variables are about the UID of this blog an
	 * empty blog and an empty path to the blog file.
	 * 
	 * @throws IOException
	 *             If the UID.xml is not writable, or something nasty occurs
	 *             while the info if been loaded.
	 */
	public StoreBlog() throws IOException {
		this.login = null;
		listBlogs = new HashMap<String, Properties>();
	}
	
	public void setOwner(String login) {
		this.login = login;
	}
	
	/**
	 * 
	 * @param blog
	 * @throws Exception
	 */
	public void addBlog(Properties blog) throws Exception {
		if (blog == null || !blog.containsKey(FlagsPost.TITLE.getFlag())
				|| !checkWord(blog.getProperty(FlagsPost.TITLE.getFlag()))) {
			
			throw new Exception("Invalid Blog.");
		}
		
		blog.setProperty(FlagsPost.OWNER.getFlag(), login);
		String title = blog.getProperty(FlagsPost.TITLE.getFlag());
		listBlogs.put(title.trim().toLowerCase().replace(' ', '_'), blog);
	}
	
	private String getPath() {
		return "Database/" + login + "/";
	}
	
	/**
	 * This method provide Singleton pattern to this class. Would be bad to the
	 * program with two different instances of this modifying the same file.
	 * 
	 * @return An instance of this class, if the class had not been
	 *         instantiated, it will do. with the class had already been
	 *         instantiated will return this instance.
	 * @throws IOException
	 */
	/*public static StoreBlog getInstance() throws IOException {
		if (thisInstance == null) {
			thisInstance = new StoreBlog();
		}
		return thisInstance;
	}*/

	public void save() throws IOException {
		Iterator<String> itTitles = listBlogs.keySet().iterator();
		while (itTitles.hasNext()) {
			String title = itTitles.next();
			final String DEFAULT = "This is the file of the blog: " + title
					+ "owned by: " + login;
			File file = new File(getPath() 
					+ title.trim().toLowerCase().replace(' ', '_') + ".xml");
			
			PrepareFiles.prepareFile((file.getAbsolutePath()), DEFAULT);
			FileOutputStream out = new FileOutputStream(file);
			listBlogs.get(title).storeToXML(out, DEFAULT);
			out.close();
		}
	}

	public void clear() throws IOException {
		login = null;
		listBlogs = new HashMap<String, Properties>();
	}
	
	private boolean checkWord(String word) {
		if(word == null || word.trim().isEmpty()) {
			return false;
		}
		return true;
	}
	
	/*public static void main(String[] args) throws Exception {
		File f = new File("kfjdh/fkjdls");
		System.out.println(f.getPath());
		
		Properties blog = new Properties();
		blog.setProperty(Flags.TITLE.getFlag(), "Test Blog Do Mal");
		blog.setProperty(Flags.DESCRIPTION.getFlag(), "Description test");
		StoreBlog manager = StoreBlog.getInstance();
		manager.setOwner("sheldon");
		System.out.println("Adicionando...");
		manager.addBlog(blog);
		System.out.println("Adicionado.");
		System.out.println("Salvando...");
		manager.save();
		System.out.println("Saved.");
	}*/
}
